<!--
 * @Author: your name
 * @Date: 2021-09-06 15:01:33
 * @LastEditTime: 2021-09-08 20:03:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ej-webui\src\pages\dashbord\index.vue
-->
<template>
  <div class="column">
    <div class="btns">
      <el-button @click="toAddHandler" type="primary" size="small"
        >添加</el-button
      >
    </div>

    <div class="content">
      <!-- 表格 -->
      <el-table :data="resData.list" style="width: 100%" aria-setsize="small">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="栏目编号" prop="id"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="图标">
          <template v-slot="scope">
            <el-popover placement="right" width="225" trigger="hover">
              <img :src="scope.row.icon" width="200" alt="图片丢失" />
              <img
                slot="reference"
                :src="scope.row.icon"
                width="50"
                alt="图片丢失"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="排序号" prop="num"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" @click="editHandler(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" @click="deleteHandler(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        layout="total,prev, pager, next,sizes"
        :total="resData.total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="pageChangeHandler"
        :page-sizes="[5, 10, 15]"
        @size-change="pageSizeChangeHandler"
      >
      </el-pagination>
      <!-- 模态框 -->
      <el-dialog
        :title="title"
        :visible.sync="visible"
        @close="dialogCloseHandler"
      >
        <el-form :model="production" :rules="rules" ref="saveForm">
          <el-form-item
            label="栏目名称"
            :label-width="formLabelWidth"
            prop="name"
          >
            <el-input v-model="production.name"></el-input>
          </el-form-item>
          <el-form-item label="序号" :label-width="formLabelWidth" prop="num">
            <el-input v-model="production.num"></el-input>
          </el-form-item>
          <el-form-item label="图标" :label-width="formLabelWidth" prop="icon">
            <el-upload
              class="avatar-uploader"
              action="http://81.69.24.232/pet/jz_upload.php"
              :show-file-list="false"
              :on-success="handlePhotoSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSavehandler">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
//@代表src
//将默认导出导入进来
import request from "@/utils/request";
import qs from "qs";
import _ from "lodash";
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 5,
      },
      resData: [],
      categories: [],
      title: "",
      visible: false,
      production: {},
      formLabelWidth: "80px",
      imageUrl: null,
      rules: {
        name: [
          { required: true, message: "栏目名称不能为空", trigger: "blur" },
        ],
        photo: [{ required: true, message: "请上传产品封面", trigger: "blur" }],
      },
    };
  },
  methods: {
    //分页查询产品表格
    async pageQueryProductions() {
      let res = await request.get("/productCategory/pageQuery", {
        params: this.params,
      });
      this.resData = res.data
    },
    //page改变事件
    pageChangeHandler(page) {
      this.params.page = page;
      this.pageQueryProductions();
    },
    //pageSize改变事件
    pageSizeChangeHandler(pageSize) {
      this.params.pageSize = pageSize;
      this.pageQueryProductions();
    },
    //新增按钮点击事件
    toAddHandler() {
      this.production = {};
      this.imageUrl = null;
      this.title = "新增栏目信息";
      this.visible = true;
    },
    // 图片上传成功的回调
    handlePhotoSuccess(res, file) {
      // 将返回的图片在线地址映射到数据模型中
      this.production.icon = res;
      this.imageUrl = res;
    },

    // 表单保存事件
    toSavehandler() {
      this.$refs["saveForm"].validate((valid) => {
        if (valid) {
          request
            .post(
              "/productCategory/saveOrUpdate",
              qs.stringify(this.production)
            )
            .then((res) => {
              this.pageQueryProductions(); // 重载数据
              this.$message.success(res.message); // 提示操作结果信息
              this.visible = false; // 关闭模态框
            });
        } else {
          return false;
        }
      });
    },
    // 模态框关闭的回调
    dialogCloseHandler() {
      this.$refs["saveForm"].resetFields(); // 置空表单验证
    },
    editHandler(row) {
      this.production = _.clone(row); //将当前行数据赋值给数据模型
      this.imageUrl = row.photo;
      this.title = "编辑栏目信息";
      this.visible = true;
    },
    //删除
    deleteHandler(id) {
      let r = confirm("确认删除吗？");
      if (r == true) {
        request.get('/productCategory/deleteById',{
            params:{id}
        }).then(res=>{
          this.pageQueryProductions()
          this.$message.success(res.message);
        })
      }  
    },
  },
  created() {
    this.pageQueryProductions(); //分页查询产品
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>>